use hyper::body::Body;
use hyper::Request;
use std::net::SocketAddr;
use thruster::context::basic_hyper_context::BasicHyperContext;
use thruster::context::typed_hyper_context::TypedHyperContext;

pub trait SocketIOContext {
    /// Returns the associated hyper request for this context.
    fn into_request(self) -> Request<Body>;

    /// Sets the status of the context.
    fn set_status(&mut self, code: u16);

    // get socket addr
    fn remote_addr(&self) -> Option<SocketAddr>;

    //get server_id
    fn server_id(&self) -> Option<String>;
}

impl SocketIOContext for BasicHyperContext {
    fn into_request(self) -> Request<Body> {
        self.hyper_request.unwrap().request
    }

    fn set_status(&mut self, code: u16) {
        self.status = code
    }

    fn remote_addr(&self) -> Option<SocketAddr> {
        self.hyper_request.as_ref().unwrap().peer_addr
    }
    fn server_id(&self) -> Option<String> {
        self.hyper_request.as_ref().unwrap().server_id.clone()
    }
}

impl<T: 'static + Send + Sync> SocketIOContext for TypedHyperContext<T> {
    fn into_request(self) -> Request<Body> {
        self.hyper_request.unwrap().request
    }
    fn set_status(&mut self, code: u16) {
        self.status = code
    }
    fn remote_addr(&self) -> Option<SocketAddr> {
        self.hyper_request.as_ref().unwrap().peer_addr
    }
    fn server_id(&self) -> Option<String> {
        self.hyper_request.as_ref().unwrap().server_id.clone()
    }
}
